/**
 * @(#)NoneedLoginUserServiceImpl.java 2017年5月22日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jsbd.boss.domian.channel.SelfMaoUser;
import org.jsbd.boss.domian.channel.ThirdUserQueryVo;
import org.jsbd.boss.enums.ApkBaseEnum;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.util.CacheKey;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service("selfMaoUserService")
public class SelfMaoUserServiceImpl extends ThirdLoginAdapterHandler<SelfMaoUser> {

    @Override
    public boolean init() {
        loginIndexKey = CacheKey.SELF_MAO_APK;
        loginType = LoginTypeEnum.SELF_MAO;
        queueKey = CacheKey.SELF_MAO_USER_QUEUE;
        return true;
    }

    @Override
    public List<SelfMaoUser> selectUserList(ThirdUserQueryVo queryVo) {
        List<SelfMaoUser> userList = new ArrayList<>();
        Integer baseId = queryVo.getBase();
        Integer apkId = queryVo.getApkId();
        Integer provinceId = queryVo.getProvinceId();
        Integer carrier = queryVo.getCarrier();
        Integer length = queryVo.getLength();
        String queueKey = getQueueKey(baseId, apkId, provinceId, carrier);
        for (int i = 0; i < length; i++) {
            Object userObj = redisClientTemp.pollFromQueue(queueKey);
            if (userObj == null) {
                break;
            }
            SelfMaoUser maoUser = JSONObject.parseObject(userObj.toString(), SelfMaoUser.class);
            maoUser.setRelease(maoUser.getReleaseVersion());
            maoUser.setNetworkOperatorName(maoUser.getNetworkOperatorname());
            maoUser.setSimOperatorName(maoUser.getSimOperatorname());
            maoUser.setNetworkExtraInfo(maoUser.getNetworkExtrainfo());
            Long joinTime = maoUser.getJoinTime();
            maoUser.setSystime(joinTime+"");

            if (clientManager.hasLogined(maoUser.getImsi(), apkId, baseId)) {
                LOGGER.warn("第三方登录 apk {} 省份{} 登录类型 {} imsi {} status {} 已经做过", new Object[]{queryVo.getApkId(), provinceId, loginType.getName(), maoUser.getImsi()});
                continue;
            }

            if(!clientManager.checkClient(maoUser)){
                continue;
            }
            //大于一分钟的去掉
            if (System.currentTimeMillis() - joinTime < SELF_MAO_USER_MAX_TIME) {
                userList.add(maoUser);
            }
        }
        return userList;

    }


    private String getQueueKey(Integer baseId, Integer apkId, Integer provinceId, Integer carrier) {
        int apkIdOrBase;
        if (baseId == null || baseId == ApkBaseEnum.OTHER.getType()) {
            apkIdOrBase = apkId;
        } else {
            apkIdOrBase = baseId;
        }
        return CacheKey.SELF_MAO_USER_DATA_QUEUE + ":" + carrier + ":" + provinceId + ":" + apkIdOrBase;
    }

}
